class AsyncSeriesWaterfallHook {
    constructor() {
        this.tasks = [];
    }
    tapAsync(name, task) {
        this.tasks.push(task);
    }
    callAsync(...args) {
        const endCb = args.pop();
        let index = 0;
        // 递归
        let next = (err, data) => {
            let task = this.tasks[index];
            if (!task || err) return endCb();
            if (index == 0) {
                task(...args, next);
            } else {
                task(data, next);
            }
            index++;
        }
        next();
    }
    tapPromise(name, task) {
        this.tasks.push(task);
    }
    promise(...args) {
        const [first, ...others] = this.tasks;
        return others.reduce((pre, next) => {
            return pre.then((data) => next(data));
        }, first(...args));
    }
}
module.exports = AsyncSeriesWaterfallHook;